﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ExcelUtils;
using TestEngine.Script;

namespace TestEngine
{

    /// <summary>
    /// Runs a TestScript updating progress as it runs.
    /// </summary>
    public class ERTestEngine
    {

        #region Constructors

        public ERTestEngine()
        {
        }

        #endregion Constructors

        #region Public Methods


        /// <summary>
        /// Runs the supplied script returning the state of the script ones execution has completed.
        /// </summary>
        /// <param name="script"></param>
        /// <param name="worker">An optional BackgroundWorker to update progress to.</param>
        /// <returns></returns>
        public ScriptState RunScript(TestScript script, Dictionary<string, string> inputs, Dictionary<string, string> outputs,  BackgroundWorker worker = null)
        {
            ScriptState state = new ScriptState();
            state.Inputs = inputs;
            state.Outputs = outputs;
            int taskNum = 0;
            if(worker != null) worker.ReportProgress(0);
            state.StartTime = DateTime.Now;
            foreach (ScriptTask task in script)
            {
                if (!task.RunTask(state))
                {
                    state.IsFailed = true;
                }
                if (worker != null) worker.ReportProgress(++taskNum / script.Count * 100);
            }
            state.EndTime = DateTime.Now;
            return state;
        }

        #endregion Public Methods

    }
}
